package jdmx.framework.service.patterns.untyped;

import java.sql.SQLException;
import java.util.ArrayList;

import jdmx.framework.common.Envelope;
import jdmx.framework.common.envelopes.EnvelopeUntyped;
import jdmx.framework.service.Dao;
import jdmx.library.da.DataRow;
import jdmx.library.da.DataTable;
import jdmx.library.da.Filter;
import jdmx.library.da.FilterField;
import jdmx.library.da.IdList;
import jdmx.library.da.SqHelper;
import jdmx.library.da.SqlParam;
import jdmx.library.da.SqlStatement;

public class DaoUntyped extends Dao {
	@Override
	public void init(String handlingCode)
	{
		super.init(handlingCode);
		//
	}
	
	@Override
	public EnvelopeUntyped create(Envelope envelope) 
	{
		/*
		 * conversia la EnvelopeUntyped
		 */		
		if (!(envelope instanceof EnvelopeUntyped))
			return null;		
		EnvelopeUntyped requestEnvelope = (EnvelopeUntyped) envelope;
		EnvelopeUntyped resultEnvelope = new EnvelopeUntyped();
		
		/*
		 * se extrag datele
		 */
		if (sqlForCreate == null)
			sqlForCreate = getSql(handlingCode, "Create");
		DataTable dataTable = requestEnvelope.getDatatable();
		IdList idList = new IdList();
		for (DataRow row : dataTable.getRows()) {
			SqlStatement sqlStatement = getSqlStatementFromDataRow(sqlForRead, row); 
			Integer id = null;
			try {
				id = db.execute(sqlStatement);
			} catch (SQLException e) {
			}
			idList.add(id);
		}		

		/*
		 * se intoarce rezultatul
		 */		
		resultEnvelope.setIdList(idList);
		return resultEnvelope;
	}

	@Override
	public EnvelopeUntyped read(Envelope envelope) 
	{
		/*
		 * conversia la EnvelopeUntyped
		 */
		if (!(envelope instanceof EnvelopeUntyped))
			return null;
		EnvelopeUntyped requestEnvelope = (EnvelopeUntyped) envelope;
		EnvelopeUntyped resultEnvelope = new EnvelopeUntyped();

		/*
		 * se extrag datele
		 */
		if (sqlForRead == null)
			sqlForRead = getSql(handlingCode, "Read") ;
		SqlStatement sqlStatement = getSqlStatementFromFilter(sqlForRead, requestEnvelope.getFilter());
		DataTable dataTable = null;
		try {
			dataTable = db.fetch(sqlStatement);
		} catch (SQLException e1) {
		}
		
		/*
		 * se intoarce rezultatul
		 */
		resultEnvelope.setDatatable(dataTable);
		return resultEnvelope;
	}
	
	@Override
	public EnvelopeUntyped update(Envelope envelope) 
	{
		/*
		 * conversia la EnvelopeUntyped
		 */		
		if (!(envelope instanceof EnvelopeUntyped))
			return null;		
		EnvelopeUntyped requestEnvelope = (EnvelopeUntyped) envelope;
		
		/*
		 * se extrag datele
		 */
		if (sqlForUpdate == null)
			sqlForUpdate = getSql(handlingCode, "Update");
		DataTable dataTable = requestEnvelope.getDatatable();
		for (DataRow row : dataTable.getRows()) {
			SqlStatement sqlStatement = getSqlStatementFromDataRow(sqlForUpdate, row); 
			try {
				db.execute(sqlStatement);
			} catch (SQLException e) {
			}
		}	
		
		return null;
	}
	
	@Override
	public EnvelopeUntyped delete(Envelope envelope)
	{
		/*
		 * conversia la EnvelopeUntyped
		 */		
		if (!(envelope instanceof EnvelopeUntyped))
			return null;		
		EnvelopeUntyped requestEnvelope = (EnvelopeUntyped) envelope;
		
		/*
		 * se extrag datele
		 */
		if (sqlForDelete == null)
			sqlForDelete = getSql(handlingCode, "Delete");
		DataTable dataTable = requestEnvelope.getDatatable();
		for (DataRow row : dataTable.getRows()) {
			SqlStatement sqlStatement = getSqlStatementFromDataRow(sqlForDelete, row); 
			try {
				db.execute(sqlStatement);
			} catch (SQLException e) {
			}
		}
		
		return null;
	}

	protected SqlStatement getSqlStatementFromFilter(String sql, Filter filter) {
		SqlStatement sqlStatement = new SqlStatement(sql);
		ArrayList<String> paramNames = SqHelper.getParamsFromSql(sql);
		for (String paramName : paramNames) {
			Object paramValue = null;
			for (FilterField filterField : filter.getFields()) {
				if (!filterField.name.equalsIgnoreCase(paramName))
					continue;
				paramValue = filterField.value;
			}
			if (paramValue == null) {
				paramValue = "%";
			}
			SqlParam sqlParam = new SqlParam(paramName, paramValue); 
			sqlStatement.paramList.add(sqlParam);
		}
		return sqlStatement;
	}

	protected SqlStatement getSqlStatementFromDataRow(String sql, DataRow dataRow) {
		SqlStatement sqlStatement = new SqlStatement(sql);
		ArrayList<String> paramNames = SqHelper.getParamsFromSql(sql);
		for (String paramName : paramNames) {
			SqlParam sqlParam = new SqlParam(paramName, dataRow.getCell(paramName)); 
			sqlStatement.paramList.add(sqlParam);
		}
		return sqlStatement;
	}
}
